package com.kmproject.haifu.mvp.presenter;

import android.Manifest;
import android.app.Application;
import android.text.TextUtils;

import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

import javax.inject.Inject;

import com.jess.arms.utils.PermissionUtil;
import com.kmproject.haifu.app.hfinterface.AliYunUploadFileInterface;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.mvp.contract.HousePushUpdateImgContract;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.ui.widget.CircleImageView;
import com.kmproject.haifu.mvp.ui.widget.Subject;

import java.io.File;
import java.util.List;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/28/2019 17:07
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class HousePushUpdateImgPresenter extends BasePresenter<HousePushUpdateImgContract.Model, HousePushUpdateImgContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    String[] permission = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA};
    private boolean isStare=false;
    @Inject
    public HousePushUpdateImgPresenter(HousePushUpdateImgContract.Model model, HousePushUpdateImgContract.View rootView) {
        super(model, rootView);
    }
    /**
     * 上传图片
     * @param
     */
    public void updateHeadImg(int index,Subject subject)
    {
        if (subject.getServerPath()!=null&&!TextUtils.isEmpty(subject.getServerPath()))
        {
            //服务器地址不用上传
            mRootView.updateImgSuccess(index,subject.getServerPath());
            return;
        }
            AppUtil.upfileToAli(mAppManager.getCurrentActivity(), Api.houseRent_server_packer, subject.getFilepath(), new AliYunUploadFileInterface() {
                @Override
                public void AliYunUploadFileCallBackProgress(long currentSize, long totalSize) {

                }

                @Override
                public void AliYunUploadFileCallBackSuccess(PutObjectResult result, String serverPath) {
//               mRootView.updHeadPortraitSuccess(serverPath);
                   mRootView.updateImgSuccess(index,serverPath);
                }

                @Override
                public void AliYunUploadFileCallBackFailure(ServiceException serviceException) {
                    mRootView.updateImgFail(index);

                }
            });


    }
    /**
     * 请求相机权限
     * type  0 从租房页面直接跳转过来的  1从选择图片页面过来的
     */
    public void requestPrisession(int type){
        //请求外部存储权限用于适配android6.0的权限管理机制
        PermissionUtil.requestPermission(new PermissionUtil.RequestPermission() {
            @Override
            public void onRequestPermissionSuccess() {
                mRootView.requestPerimissionSuccess(type);
            }

            @Override
            public void onRequestPermissionFailure(List<String> permissions) {
                mRootView.showMessage("请求相机权限失败");
            }

            @Override
            public void onRequestPermissionFailureWithAskNeverAgain(List<String> permissions) {
                mRootView.showMessage("请同意使用摄像头权限");
            }
        }, mRootView.getPermissions(), mErrorHandler, permission);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
